import { Module } from "vuex";

const banner = {
  type: "banner",
  id: 1,
  banners: [{ img: "http://thirdwx.qlogo.cn/mmopen/vi_32/qh1wmJHk0LMSnj3cVT7QlRbNJNoyianTia7LcQYTniclDHuTer7cqXEicg3Wg8BXRLPR5BzRKSM8ibvct3PBrnciakrA/132", link: "", title: "测试" }],
  padding: 0,
  imageStyle: "custom",
  angle: "custom",
  indicator: "1",
};

const module: Module<any, any> = {
  namespaced: true,
  state: {
    // 组件列表
    components: [banner],
    // 设置当前组件
    currentComponent: "",
  },
  mutations: {
    // 设置组件列表
    SET_COMPONENTS(state, payload) {
      state.components = payload;
    },
    // 设置当前组件
    SET_CURRENT_COMPNENT(state, payload) {
      state.currentComponent = payload;
    },
  },
  actions: {
    /**
     * 添加元素
     * @param component
     */
    addComponent({ commit, state }, component = banner) {
      commit(
        "SET_COMPONENTS",
        state.components.concat(
          Object.assign({}, component, { id: +new Date() }),
        ),
      );
    },
    /**
     * 更新元素
     * @param component
     */
    updateComponent({ commit, state }, component) {
      const list = state.components.map(item =>
        item.id === component.id ? component : item,
      );
      commit("SET_COMPONENTS", list);
    },
  },
};

export default module;
